package com.aigou.admin.aigouspringboot.controller.admin;

import com.aigou.admin.aigouspringboot.common.LogScope;
import com.aigou.admin.aigouspringboot.entity.User;
import com.aigou.admin.aigouspringboot.entity.dto.UserQueryDTO;
import com.aigou.admin.aigouspringboot.result.Result;
import com.aigou.admin.aigouspringboot.service.UserService;
import com.aigou.admin.aigouspringboot.utils.RandomUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
/**
 * @Author Mr.LiFuXing
 * @Date 2024/12/10 20:22
 * @Title UserController
 * @Package com.aigou.admin.aigouspringboot.controller
 */

@RestController("adminUserController")
@RequestMapping("/admin/user")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 分页查询
     *
     * @param current 当前页
     * @return info
     */
    @GetMapping
    @LogScope(title = "用户管理模块", content = "查看用户列表信息")
    public Result select(@RequestParam Integer current, UserQueryDTO userQueryDTO) {

        log.info("查询第{}页的用户信息", current);
        // 分页查询 都返回PageInfo(分页插件工具)
        PageInfo<User> userPageInfo = userService.selectUserListByPage(current,userQueryDTO);
        return Result.success(userPageInfo);
    }

    @GetMapping("/{id}")
    @LogScope(title = "用户管理模块", content = "根据id查询用户信息")
    public Result detail(@PathVariable String id){

        User user = userService.selectById(id);
        return Result.success(user);
    }

    @DeleteMapping("/{id}")
    @LogScope(title = "用户管理模块", content = "根据id删除用户信息")
    public Result delete(@PathVariable String id){

        int i = userService.deleteById(id);
        return i>0 ? Result.success() : Result.error();
    }



    /**
     * 编辑用户
     * @param user user
     * @return r
     */
    @PutMapping
    @LogScope(title = "用户管理模块", content = "编辑用户信息")
    public Result edit(@RequestBody User user){

        int i = userService.edit(user);
        return i>0 ? Result.success() : Result.error();
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @PostMapping("/save")
    @LogScope(title = "用户管理模块", content = "添加用户信息")
    public Result save(@RequestBody User user){

        user.setUserId(RandomUtil.getRandomNumber());
        int i = userService.save(user);
        return i>0 ? Result.success() : Result.error();
    }
}
